﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Web.Script.Services;
using System.Web.Services;
using CE.Database;

public partial class car_insurance_proposalforms_getaddress : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static string BindDistrict(string provinceid ,string language)
    {
        string _language = "EN";
        string _strSelect = "Select";
        if (language == "th-TH")
        {
            _language = "TH";
            _strSelect = "เลือก";
        }

        MyDatabase _db = new MyDatabase();
        DataTable _dt = new DataTable();
        SortedList _sPara = new SortedList();
        _sPara.Add("@ProvinceID", provinceid);
        _dt = _db.GetDataTableBySP("sprocTHDistrictSelectItemList", _sPara);
        _dt.DefaultView.Sort = _language;
        _db = null;
        StringWriter builder = new StringWriter();

        builder.WriteLine("[");
        if (_dt.Rows.Count > 0)
        {
            builder.WriteLine("{\"optionDisplay\":\"" + _strSelect + "\",");
            builder.WriteLine("\"optionValue\":\"-1\"},");
            for (int i = 0; i <= _dt.Rows.Count - 1; i++)
            {
                builder.WriteLine("{\"optionDisplay\":\"" + _dt.Rows[i][_language] + "\",");
                builder.WriteLine("\"optionValue\":\"" + _dt.Rows[i]["DistrictID"] + "\"},");
            }
        }
        else
        {
            builder.WriteLine("{\"optionDisplay\":\"" + _strSelect + "\",");
            builder.WriteLine("\"optionValue\":\"-1\"},");
        }
        string returnjson = builder.ToString().Substring(0, builder.ToString().Length - 3);
        returnjson = returnjson + "]";
        return returnjson.Replace("\r", "").Replace("\n", "");
    }

    [WebMethod]
    public static string BindSubDistrict(string districtid, string language)
    {
        string _language = "EN";
        string _strSelect = "Select";
        if (language == "th-TH")
        {
            _language = "TH";
            _strSelect = "เลือก";
        }

        MyDatabase _db = new MyDatabase();
        DataTable _dt = new DataTable();
        SortedList _sPara = new SortedList();
        _sPara.Add("@DistrictID", districtid);
        _dt = _db.GetDataTableBySP("sprocTHSubDistrictSelectItemList", _sPara);
        _dt.DefaultView.Sort = _language;
        _db = null;
        StringWriter builder = new StringWriter();

        builder.WriteLine("[");
        if (_dt.Rows.Count > 0)
        {
            builder.WriteLine("{\"optionDisplay\":\"" + _strSelect + "\",");
            builder.WriteLine("\"optionValue\":\"-1\"},");
            for (int i = 0; i <= _dt.Rows.Count - 1; i++)
            {
                builder.WriteLine("{\"optionDisplay\":\"" + _dt.Rows[i][_language] + "\",");
                builder.WriteLine("\"optionValue\":\"" + _dt.Rows[i]["SubDistrictID"] + "\"},");
            }
        }
        else
        {
            builder.WriteLine("{\"optionDisplay\":\"" + _strSelect + "\",");
            builder.WriteLine("\"optionValue\":\"-1\"},");
        }
        string returnjson = builder.ToString().Substring(0, builder.ToString().Length - 3);
        returnjson = returnjson + "]";
        return returnjson.Replace("\r", "").Replace("\n", "");
    }

     [WebMethod]
    public static string SetPostCode(string subdistrictid)
     {
        MyDatabase _db = new MyDatabase();
        DataTable _dt = new DataTable();
        SortedList _sPara = new SortedList();
        _sPara.Add("@SubDistrictID", subdistrictid);
        _dt = _db.GetDataTableBySP("sprocTHPostCodeSelectItemList", _sPara);
        _db = null;
        string postcode = "";
        if (_dt.Rows.Count > 0)
        {
            postcode = _dt.Rows[0]["PostCode"].ToString();
        }
        return postcode;
    }
    
}
